package com.commerceerp.system.controller;

import com.commerceerp.system.api.winit.model.OutboundOrder;
import com.commerceerp.system.entity.LogisticsWinit;
import com.commerceerp.system.service.LoginService;
import com.commerceerp.system.service.WinitService;
import com.commerceerp.system.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Api(description = "万邑通物流管理", value = "万邑通物流管页面")
@Controller
@RequestMapping("winit")
public class WinitController {

    @Value("WINIT_URL")
    private String WINIT_URL;

    @Value("WINIT_CLIENT_ID")
    private String WINIT_CLIENT_ID;

    @Autowired
    private LoginService loginService;

    @Autowired
    private WinitService winitService;

    //万邑通物流编辑
    @ApiOperation(value = "万邑通物流添加")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/form", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult form(LogisticsWinit logisticsWinit, HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if (to.getCode() != 200) {
            return to;
        }
        logisticsWinit.setUserId((String) to.getData());
        return winitService.form(logisticsWinit);
    }

    @ApiOperation(value = "万邑通仓库列表")
    @RequestMapping(value = "/getWarehouses", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getWarehouses() {

        return winitService.getWarehouse();
    }

    @ApiOperation(value = "仓库库存信息")
    @RequestMapping(value = "/getStock", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "warehouseId", value = "仓库id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "sku", value = "sku", dataType = "String"),
    })
    @ResponseBody
    public ApiResult getStock(String warehouseId,String sku,HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if (to.getCode() != 200) {
            return to;
        }
        String userId = (String) to.getData();
        return winitService.getStock(warehouseId,sku,userId);
    }

    @ApiOperation(value = "运费信息")
    @RequestMapping(value = "/getFreight", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", value = "orderId", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "country", value = "国家", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "region", value = "州", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zipCode", value = "邮编", required = true, dataType = "String"),
    })
    @ResponseBody
    public ApiResult getFreight(String orderId,String country,String region,String city, String zipCode,HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if (to.getCode() != 200) {
            return to;
        }
        String userId = (String) to.getData();
        return winitService.getFreight(orderId,userId,country,region,city,zipCode);
    }

    @ApiOperation(value = "创建出库单")
    @RequestMapping(value = "/createOutboundOrder", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "warehouseID", value = "万邑通海外仓ID",  required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "eBayOrderID", value = "eBay OrderID", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deliveryWayID", value = "最后一公里派送方式ID",  required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "insuranceTypeID", value = "最后一公里派送方式所用保险类型ID",  required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "sellerOrderNo", value = "卖家订单号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "recipientName", value = "收件人姓名",  required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "phoneNum", value = "收件人电话",  required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zipCode", value = "邮编", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "emailAddress", value = "收件人电子邮箱",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "country", value = "国家", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "region", value = "州", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "address1", value = "收件人街道1",  required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "address2", value = "收件人街道2", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "doorplateNumbers", value = "门牌号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "VATNo", value = "卖家VAT号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderId", value = "orderId", required = true, dataType = "Integer"),
    })
    @ResponseBody
    public ApiResult createOutboundOrder(OutboundOrder outboundOrder, HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if (to.getCode() != 200) {
            return to;
        }
        String userId = (String) to.getData();
        return winitService.createOutboundOrder(outboundOrder,userId);
    }

    //获取万邑联授权码
    @ApiOperation(value = "删除万邑联授权码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/delWinitToken", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delWinitToken(String id, HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if (to.getCode() != 200) {
            return to;
        }
        String userId = (String)to.getData();
        return winitService.delWinitToken(id,userId);
    }
}
